local extension = Package("lzsj3")
extension.extensionName = "lzsj"

local U = require "packages/utility/utility"
local DIY = require "packages/diy_utility/diy_utility"
local ls = require 'packages/lvshisha/utility/lvshi_util'
local TT = require "packages/tuntian_studio/utility"

Fk:loadTranslationTable{
  ["lzsj3"] = "雷组-二刺螈",
}


local marchseven = General(extension, "lzsj__marchseven", "ev", 4, 4, General.Female)

local mengjizhengyi = fk.CreateActiveSkill{
  name = "lzsj__mengjizhengyi",
  prompt = "#lzsj__mengjizhengyi",
  mute = true,
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.TrueFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:notifySkillInvoked(player, self.name, "support")
    player:broadcastSkillInvoke(self.name, table.random({1,2}))
    target:drawCards(1, self.name)
    local id = room:askForCardChosen(player, target, "hej", self.name)
    if id then
      target:addToPile("shield", id, true, self.name)
    end
  end,
}

local mengjizhengyi_trigger = fk.CreateTriggerSkill{
  name = "#lzsj__mengjizhengyi_trigger",
  mute = true,
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and player:getMark("#mengjizhengyi-turn") > 0
  end,
  on_cost = function(self, event, target, player, data)
    --return player.room:askForSkillInvoke(player, "lzsj__mengjizhengyi", nil, "#mengjizhengyi-slash::"..target.id)
    local cards = player.room:askForCard(player, 1, 1, true, self.name, true, ".", "#mengjizhengyi-slash::"..target.id)
    if #cards > 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, "lzsj__mengjizhengyi", "offensive")
    player:broadcastSkillInvoke("lzsj__mengjizhengyi", table.random({3, 4}))
    player.room:useVirtualCard("lvs_ice__slash", self.cost_data, player, target, self.name, true)
  end,

  refresh_events = {"fk.ShieldCosted"},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "#mengjizhengyi-turn")
  end,
}

local bingkejianyu = fk.CreateViewAsSkill{
  name = "lzsj__bingkejianyu",
  mute = true,
  prompt = "#lzsj__bingkejianyu",
  pattern = "archery_attack",
  enabled_at_play = function(self, player)
    return player:getMark("@@lvshi_readying:::"..self.name) == 0
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    if #cards ~= 0 then return nil end
    local c = Fk:cloneCard("archery_attack")
    c.skillName = self.name
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    player:broadcastSkillInvoke(self.name, table.random({1,2}))
    room:notifySkillInvoked(player, self.name)
  end,
  after_use = function(self, player, use)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p) return p:isKongcheng() end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 99, "#bingkejianyu-choose", self.name, true)
    if #tos > 0 then
      player:broadcastSkillInvoke(self.name, table.random({3,4}))
      for _, p in ipairs(tos) do
        room:getPlayerById(p):setChainState(true)
      end
    end
  end,
}

local bingkejianyu_trigger = fk.CreateTriggerSkill{
  name = "#lzsj__bingkejianyu_trigger",
  mute = true,
  events = {fk.PreDamage},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name, true, true) and data.card and data.card.skillName == "lzsj__bingkejianyu"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.damageType = fk.IceDamage
  end
}

mengjizhengyi:addRelatedSkill(mengjizhengyi_trigger)
bingkejianyu:addRelatedSkill(bingkejianyu_trigger)
bingkejianyu.XushiSkill = true
marchseven:addSkill(mengjizhengyi)
marchseven:addSkill(bingkejianyu)

Fk:loadTranslationTable{
  ["lzsj__marchseven"] = "三月七",
  ["designer:lzsj__marchseven"] = "懂太师",
  ["lzsj__mengjizhengyi"] = "可爱即正义",
  [":lzsj__mengjizhengyi"] = "阶段技，你可令一名角色摸一张牌，将其区域内的一张牌置为其护甲。其他角色的回合结束后，若此回合有角色消耗护甲，你可将一张牌当冰【杀】对当前回合角色使用。",
  ["#lzsj__mengjizhengyi"] = "你可选择一名角色",
  ["#mengjizhengyi-slash"] = "你可选择一张牌当冰【杀】对%dest使用",
  ["lzsj__bingkejianyu"] = "冰刻箭雨时",
  ["#lzsj__bingkejianyu_prohibit"] = "冰刻箭雨",
  ["@@lvshi_readying:::lzsj__bingkejianyu"] = "冰刻箭雨行置",
  ["#lzsj__bingkejianyu"] = "你可视为使用冰【万箭齐发】",
  [":lzsj__bingkejianyu"] = "<a href='lvshi_xushi_skill'>蓄势技</a>，出牌阶段，你可视为使用冰【万箭齐发】，然后你可横置任意名没有手牌的角色。",
  ["#bingkejianyu-choose"] = "你可横置任意名没有手牌的角色",

  ["$lzsj__mengjizhengyi1"] = "本姑娘出马，怎么可能会输嘛～",
  ["$lzsj__mengjizhengyi2"] = "乖乖站好，这就给你加个祝福～",
  ["$lzsj__mengjizhengyi3"] = "桃花影落飞神剑～",
  ["$lzsj__mengjizhengyi4"] = "来尝尝本姑娘的厉害～",
  ["$lzsj__bingkejianyu1"] = "我一个可以打十个！",
  ["$lzsj__bingkejianyu2"] = "左青龙，右白虎，少吃碳水化合物——看招！",
  ["$lzsj__bingkejianyu3"] = "冻结吧！",
  ["$lzsj__bingkejianyu4"] = "别小瞧本姑娘！",
  ["~lzsj__marchseven"] = "呀啊！不讲武德。。。",
}

local Kurumi = General(extension, "lzsj__kurumi", "ev", 3, 3, General.Female)

local reinforcementsCardSkill = fk.CreateActiveSkill{
  name = "reinforcements_skill",
  prompt = "#reinforcements_skill",
  can_use = Util.CanUse,
  min_target_num = 1,
  max_target_num = 2,
  mod_target_filter = function(self, to_select, selected, user)
    return Fk:currentRoom():getPlayerById(to_select):isWounded()
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(_, room, cardEffectEvent)
    local to = room:getPlayerById(cardEffectEvent.to)
    room:recover({
      who = to,
      num = 1,
      recoverBy = room:getPlayerById(cardEffectEvent.from),
      skillName = "reinforcements"
    })
  end,
}

local reinforcements = fk.CreateTrickCard{
  name = "&reinforcements",
  skill = reinforcementsCardSkill,
  multiple_targets = true,
  is_derived = true,
}

reinforcements.package = extension
Fk:addSkill(reinforcementsCardSkill)
Fk:addCard(reinforcements)

local lzsj__kekedi = fk.CreateTriggerSkill{
  name = "lzsj__kekedi",
  anim_type = "drawcard",
  events ={fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askForChoice(player, {"top", "bottom"}, self.name)
    local n = 3
    if player:getMark("kekekdi_buff") > 0 then
      n = 3 * 2 ^ player:getMark("kekekdi_buff")
    end
    local cards = room:getNCards(n, choice)
    room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true)
    local cards_ids = table.simpleClone(cards)
    while #cards_ids > 0 do
      local id = room:askForCardChosen(player, player, { card_data = {{self.name, cards_ids}} }, self.name)
      if not id then id = cards_ids[1] end
      table.removeOne(cards_ids, id)
      local card = Fk:getCardById(id)
      local pattern
      if card.suit == Card.Spade then
        pattern = "foresight"
      elseif card.suit == Card.Club then
        pattern = "savage_assault"
      elseif card.suit == Card.Heart then
        pattern = "lvshi__indulgence"
      elseif card.suit == Card.Diamond then
        pattern = "reinforcements"
      end
      --local subcards = pattern == "lvshi__indulgence" and { id } or nil
      local subcards = { id }
      if pattern ~= "lvshi__indulgence" then subcards = nil end
      U.askForUseVirtualCard(room, player, pattern, subcards, self.name)
      if #table.filter(cards, function (id) return card.suit == Fk:getCardById(id).suit end) ~= 1 then
        break
      end
    end
    if #cards_ids == 0 then
      local drawpile = table.simpleClone(room.draw_pile)
      room:moveCardTo(drawpile, Card.DiscardPile, nil, fk.ReasonPut, self.name, true)
    end
  end,
}

local shishizhicheng = fk.CreateTriggerSkill{
  name = "shishizhicheng",
  events = {fk.TurnEnd, fk.AfterDrawPileShuffle},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return player:hasSkill(self) and (target == player or table.find(target:getCardIds("ej"), function (id)
        return Fk:getCardById(id).color == Card.Black end)) and player.room:getBanner("@$CenterArea")
    else
      return player:hasSkill(self) and player:getMark("kekekdi_buff") < 2
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return player.room:askForSkillInvoke(player, self.name)
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      local cards = room:getBanner("@$CenterArea")
      local n = math.min(#cards, 3)
      local ids = room:askForCardsChosen(player, player, 1, n, {card_data = {{self.name, cards}}}, self.name, "#shishizhicheng-prompt")
      --room:moveCardTo(ids, Card.DrawPile, nil, fk.ReasonPut, self.name, nil)
      --room:askForGuanxing(player, room:getNCards(#ids))
      local card_ids = room:askForGuanxing(player, ids, {0, #ids},  {0, #ids}, self.name, true)
      local to_top = table.reverse(card_ids.top)
      local to_bottom = card_ids.bottom
      local move1 = {
        ids = to_top,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = self.name,
      }
      local move2 = {
        ids = to_bottom,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = self.name,
        drawPilePosition = -1,
      }
      room:moveCards(move1, move2)
    else
      room:loseHp(player, 1, self.name)
      room:addPlayerMark(player, "kekekdi_buff")
    end
  end,
}

shishizhicheng.CenterArea = true
Kurumi:addSkill(lzsj__kekedi)
Kurumi:addSkill(shishizhicheng)

Fk:loadTranslationTable{
  ["top"] = "牌堆顶",
  ["bottom"] = "牌堆底",
  ["lzsj__kurumi"] = "时崎狂三",
  ["#lzsj__kurumi"] = "溯时觅光",
  ["designer:lzsj__kurumi"] = "子规",
  ["lzsj__kekedi"] = "刻刻帝",
  [":lzsj__kekedi"] = "出牌阶段开始时，你可弃置牌堆一侧的三张牌，选择其中一张黑桃/梅花/方片/红桃牌并视为使用【洞烛先机】/【南蛮入侵】/<a href=':reinforcements'>【援军】</a>/【乐不思蜀】，若此牌为唯一花色，你重复此流程且不能再选择此牌；若你选择了所有牌，你弃置牌堆所有牌。",
  --["#kekedi_swap"] = "你可以弃置牌堆<font color='#DE3163'><b>小心中毒！</b></font>",
  ["shishizhicheng"] = "食时之城",
  [":shishizhicheng"] = "一名角色的回合结束时，若其为你，或其场上有黑色牌，你可将中央区内至多三张牌置于牌堆顶或牌堆底。牌堆前两次洗切后，你失去1点体力，然后“刻刻帝”亮出牌数翻倍。",
  ["#shishizhicheng-prompt"] = "食时之城：请选择至多三张牌",

  ["reinforcements"] = "援军",
  [":reinforcements"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一至两名已受伤的角色<br/><b>效果</b>：目标角色回复1点体力。",
}

local RuanMei = General(extension, "lzsj__ruanmei", "ev", 3, 3, General.Female)

local lzsj__canmeizhan = fk.CreateTriggerSkill{
  name = "lzsj__canmeizhan",
  anim_type = "control",
  events = {fk.BeforeDrawCard, fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    if event == fk.BeforeDrawCard then
      return player:hasSkill(self) and player:getMark("@@lzsj__canmeizhan") == 0 and target.phase ~= Player.Draw and target ~= player
    else
      return player:hasSkill(self) and player:getTableMark("@@lzsj__canmeizhan")[1] == target.id
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.BeforeDrawCard then
      --return player.room:askForSkillInvoke(player, self.name, nil, "#lzsj__canmeizhan:"..target.id.."::"..data.num)
      local cards = player.room:askForCard(player, 1, 1, true, self.name, true, ".|.|club", "#lzsj__canmeizhan:"..target.id.."::"..data.num, self.name)
      if #cards > 0 then
        self.cost_data = cards
        return true
      end
    else
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    if event == fk.BeforeDrawCard then
      player.room:recastCard(self.cost_data, player, self.name)
      local n = data.num
      player.room:setPlayerMark(player, "@@lzsj__canmeizhan", {target.id, n})
      data.num = 0
    else
      target:drawCards(player:getTableMark("@@lzsj__canmeizhan")[2], self.name)
    end
    --return true
  end,

  refresh_events = {fk.AfterTurnEnd, fk.Death, fk.DrawNCards},
  can_refresh = function (self, event, target, player, data)
    if event == fk.AfterTurnEnd or event == fk.Death then
      if player:hasSkill(self) and player:getMark("@@lzsj__canmeizhan") ~= 0 then
        if event == fk.AfterTurnEnd then
          return player:getTableMark("@@lzsj__canmeizhan")[1] == target.id
        else
          return player:getTableMark("@@lzsj__canmeizhan")[1] == data.who
        end
      end
    else
      return target == player and player:hasSkill(self.name) and player:getMark("@@lzsj__canmeizhan") ~= 0 and not table.contains(player:getTableMark("lzsj__mofutiao"), "lzsj__mofutiao1")
    end
  end,
  on_refresh = function (self, event, target, player, data)
    if event == fk.AfterTurnEnd or event == fk.Death then
      player.room:setPlayerMark(player, "@@lzsj__canmeizhan", 0)
    else
      data.n = data.n + 1
    end
  end,
}

local canmeizhan_maxcards = fk.CreateMaxCardsSkill {
  name = "#lzsj__canmeizhan_maxcards",
  correct_func = function(self, player)
    if player:getMark("@@lzsj__canmeizhan") ~= 0 and not table.contains(player:getTableMark("lzsj__mofutiao"), "lzsj__mofutiao2") then
      return 1
    end
  end,
}

local canmeizhan_targetmod = fk.CreateAttackRangeSkill{
  name = "#canmeizhan_attackrange",
  correct_func = function (self, from, to)
    if from:getMark("@@lzsj__canmeizhan") ~= 0 and not table.contains(from:getTableMark("lzsj__mofutiao"), "lzsj__mofutiao3") then
      return 1
    end
  end,
}

local function Dochoiceeffect(target, choice, skillName)
  if choice == "lzsj__mofutiao1" then
    target:drawCards(2, skillName)
  elseif choice == "lzsj__mofutiao2" then
    target.room:damage{
      from = player,
      to = target,
      damage = 1,
      damageType = fk.NormalDamage,
      skillName = skillName,
    }
  elseif choice == "lzsj__mofutiao3" then
    target.room:recover{ who = target, num = 1, skillName = skillName }
  end
end

local lzsj__mofutiao = fk.CreateActiveSkill{
  name = "lzsj__mofutiao",
  anim_type = "support",
  prompt = "#lzsj__mofutiao",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.TrueFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local choices = {"lzsj__mofutiao1","lzsj__mofutiao2","lzsj__mofutiao3"}
    local all_choices = {"lzsj__mofutiao1","lzsj__mofutiao2","lzsj__mofutiao3"}
    local mark = player:getTableMark("lzsj__mofutiao")
    for _, i in ipairs(mark) do
      table.removeOne(choices, i)
    end
    local choice = room:askForChoice(player, choices, self.name, nil, false, all_choices)
    table.insertIfNeed(mark, choice)
    Dochoiceeffect(target, choice, self.name)
    if #mark >= 3 then
      room:setPlayerMark(player, "lzsj__mofutiao", {})
      Dochoiceeffect(target, choice, self.name)
    else
      room:setPlayerMark(player, "lzsj__mofutiao", mark)
    end
  end,
}

lzsj__canmeizhan:addRelatedSkill(canmeizhan_targetmod)
lzsj__canmeizhan:addRelatedSkill(canmeizhan_maxcards)
RuanMei:addSkill(lzsj__canmeizhan)
RuanMei:addSkill(lzsj__mofutiao)

Fk:loadTranslationTable{
  ["lzsj__ruanmei"] = "阮·梅",
  ["designer:lzsj__ruanmei"] = "请叫我宣宣",
  ["lzsj__canmeizhan"] = "残梅绽",
  ["@@lzsj__canmeizhan"] = "残梅绽",
  ["#lzsj__canmeizhan"] = "你可重铸一张梅花牌令%src此次摸%arg张牌延迟至其回合结束",
  [":lzsj__canmeizhan"] = "当其他角色于摸牌阶段外摸牌时，你可重铸一张梅花牌，改为令其回合结束后摸等量的牌，期间你不能发动此技能，且摸牌阶段摸牌数、攻击范围和手牌上限均+1。",
  ["lzsj__mofutiao"] = "抹复挑",
  [":lzsj__mofutiao"] = "阶段技，你可删去“残梅绽”中第一/二/三项效果，然后令一名角色摸两张牌/回复1点体力/分配1点伤害；额外，当最后一项被删去时，你额外执行一次对应效果并恢复所有项。",
  ["#lzsj__mofutiao"] = "抹复挑：你可以选择一名角色",
  ["lzsj__mofutiao1"] = "摸两张牌",
  ["lzsj__mofutiao2"] = "受到1点伤害",
  ["lzsj__mofutiao3"] = "回复1点体力",

  ["$lzsj__canmeizhan1"] = "生命里的每一片花瓣……",
  ["$lzsj__canmeizhan2"] = "无论何时盛放，都会有被风吹落的…那一天。",
  ["$lzsj__mofutiao1"] = "琴音周而复始。",
  ["$lzsj__mofutiao2"] = "等待亦是变化。",
  ["~lzsj__ruanmei"] = "还没有…答案……",
}

local Satoru = General(extension, "lzsj__satoru", "ev", 4)

local lzsj__liuyan = fk.CreateVisibilitySkill{
  name = 'lzsj__liuyan',
  frequency = Skill.Compulsory,
  card_visible = function(self, player, card)
    if player:hasSkill(self) and Fk:currentRoom():getCardArea(card) == Card.PlayerHand
    and (player.phase ~= Player.NotActive or player:getMark("lzsj__wuxian") > 0) then
      return true
    end
  end
}

local lzsj__wuxian = fk.CreateTriggerSkill{
  name = "lzsj__wuxian",
  events = {fk.RoundStart, fk.RoundEnd, fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    if event == fk.RoundStart then
      return player:hasSkill(self)
    elseif event == fk.RoundEnd then
      return player:hasSkill(self) and #player:getTableMark("lzsj__wuxian-round") > 0
    else
      return player:hasSkill(self) and target == player and table.contains(player:getTableMark("@$lzsj__wuxian"), data.card.name)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      room:addPlayerMark(player, "lzsj__wuxian")
      local all_names = U.getAllCardNames("btd", false)
      local names = table.simpleClone(all_names)
      if #player:getTableMark("@$lzsj__wuxian") ~= 0 then
        for _, name in ipairs(player:getTableMark("@$lzsj__wuxian")) do
          table.removeOne(names, name)
        end
        room:setPlayerMark(player, "@$lzsj__wuxian", 0)
      end
      local mark = U.askForChooseCardNames(room, player, names, 3, 3, self.name, "#lzsj__wuxian-choice", all_names, true, false)
      if #mark > 0 then
        room:setPlayerMark(player, "@$lzsj__wuxian", mark)
        room:setPlayerMark(player, "lzsj__wuxian-round", mark)
      end
      room:setPlayerMark(player, "lzsj__wuxian", 0)
    elseif event == fk.RoundEnd then
      local n = #player:getTableMark("lzsj__wuxian-round")
      if n > 0 then
        local targets = table.map(table.filter(room.alive_players, function (p) return not p:isNude() end), Util.IdMapper)
        local tos = room:askForChoosePlayers(player, targets, 1, n, "#lzsj__wuxian:::"..n, self.name, true)
        if #tos > 0 then
          for _, p in ipairs(tos) do
            local id = room:askForCardChosen(player, room:getPlayerById(p), "he", self.name)
            room:moveCardTo(id, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, player.id)
          end
        end
        if n == 3 and not player.dead then
          local victim = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#lzsj__wuxian-damage", self.name, false)
          if #victim > 0 then
            room:damage({
              from = player,
              to = room:getPlayerById(victim[1]),
              damage = 1,
              damageType = fk.NormalDamage,
              skillName = self.name,
            })
          end
        end
      end
    else
      AimGroup:cancelTarget(data, player.id)
      local mark = player:getTableMark("lzsj__wuxian-round")
      local gain = player:getTableMark("@$lzsj__wuxian-round")
      if table.contains(mark, data.card.name) then
        table.removeOne(mark, data.card.name)
        table.insertIfNeed(gain, data.card.name)
      end
      room:setPlayerMark(player, "lzsj__wuxian-round", mark)
      room:setPlayerMark(player, "@$lzsj__wuxian-round", gain)
    end
  end,
}


Satoru:addSkill(lzsj__liuyan)
Satoru:addSkill(lzsj__wuxian)

Fk:loadTranslationTable{
  ["lzsj__satoru"] = "五条悟",
  ["designer:lzsj__satoru"] = "子规",
  ["lzsj__liuyan"] = "六眼",
  [":lzsj__liuyan"] = "锁定技，你的回合内，或你发动“无下限术式”时，其他角色的手牌对你可见。",
  ["lzsj__wuxian"] = "无下限术式",
  [":lzsj__wuxian"] = "轮次开始时，你声明三个上轮未选择过的牌名，若如此做，此轮有角色使用你声明的牌指定你为目标时，取消之；轮次结束时，若你此轮有牌名未取消过目标，你可弃置至多等量名角色各一张牌，若为所有牌名，则你再分配1点伤害。",
  ["#lzsj__wuxian-choice"] = "你可选择3次牌名，有角色使用你声明的牌指定你为目标时，取消之",
  ["@$lzsj__wuxian"] = "无下限术式",
  ["#lzsj__wuxian"] = "请选择至多%arg名角色，你弃置这些角色各一张牌",
  ["#lzsj__wuxian-damage"] = "请分配1点伤害",
  ["@$lzsj__wuxian-round"] = "此轮已无效",
}

local hakfoo = General(extension, "lzsj__hakfoo", "ev", 4)

local function HakfooSkills(player, name, data)
  player.room:notifySkillInvoked(player, name, "big")
  player.room:delay(500)
  data.extra_data = data.extra_data or {}
  player.room:addPlayerMark(player, name.."-turn")
  if name == "heihutaoxin" then
    local to = player.room:getPlayerById(data.tos[1][1])
    if not to:isKongcheng() then
      local id = player.room:askForCardChosen(player, to, "h", "lzsj__zoubian")
      player.room:moveCardTo(id, Card.PlayerHand, player, fk.ReasonPrey, "lzsj__zoubian", "", false, player.id)
    end
  elseif name == "jufukandashu" then
    data.additionalEffect = (data.additionalEffect or 0) + 1
  elseif name == "yihushaliangyang" then
    data.extra_data.yihushaliangyang = (data.extra_data.yihushaliangyang or 0) + 1
  elseif name == "longjuanfeng" then
    data.extra_data.longjuanfeng = (data.extra_data.longjuanfeng or 0) + 1
  elseif name == "fulaminggewubu" then
    local current = player.room.current
    if #current:getCardIds("ej") > 0 then
      local targets = table.map(table.filter(player.room:getOtherPlayers(current), function(p)
        return current:canMoveCardsInBoardTo(p) end), Util.IdMapper)
      if #targets > 0 then
        local to = player.room:askForChoosePlayers(player, targets, 1, 1, "#fulaminggewubu-ask:"..current.id, "lzsj__zoubian", true)
        if #to > 0 then
          player.room:askForMoveCardInBoard(player, current, player.room:getPlayerById(to[1]), "lzsj__zoubian", nil, current)
        end
      end
    end
  end
  player:setCardUseHistory(data.card.trueName, 0, Player.HistoryPhase)
end

local lzsj__zoubian = fk.CreateTriggerSkill{
  name = "lzsj__zoubian",
  mute = true,
  events = {fk.CardUsing, fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and #player:getTableMark("@$lzsj__zoubian") >= 2
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUsing then
      local mark = player:getTableMark("@$lzsj__zoubian")
      local carda = Fk:getCardById(mark[#mark - 1])
      local cardb = Fk:getCardById(mark[#mark])
      local cardnames = {"savage_assault", "archery_attack", "god_salvation", "iron_chain", "lure_tiger", "await_exhausted", "collaborate"}
      local heihutaoxin = cardb.trueName == "slash" and carda.suit == Card.Spade and player:getMark("heihutaoxin-turn") == 0
      local jufukandashu = cardb.trueName == "slash" and carda.type == Card.TypeEquip and player:getMark("jufukandashu-turn") == 0
      local yihushaliangyang = (cardb.type == Card.TypeBasic or cardb:isCommonTrick()) and
      not table.contains(cardnames, cardb.trueName) and carda.color == Card.Red and player:getMark("yihushaliangyang-turn") == 0
      local longjuanfeng = (cardb.type == Card.TypeBasic or cardb:isCommonTrick()) and (carda.type == Card.TypeBasic or carda:isCommonTrick())
      and player:getMark("longjuanfeng-turn") == 0
      local fulaminggewubu = data.extra_data and data.extra_data.fulaminggewubu and player:getMark("fulaminggewubu-turn") == 0
      local wuyazuofeiji = data.extra_data and data.extra_data.wuyazuofeiji and player:getMark("wuyazuofeiji-turn") == 0
      self.cost_data = {heihutaoxin, jufukandashu, yihushaliangyang, longjuanfeng, fulaminggewubu, wuyazuofeiji}
      return table.find(self.cost_data, function (bool) return bool == true end) and player.room:askForSkillInvoke(player, self.name)
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.CardUsing then
      if self.cost_data[1] then
        HakfooSkills(player, "heihutaoxin", data)
      end
      if self.cost_data[2] then
        HakfooSkills(player, "jufukandashu", data)
      end
      if self.cost_data[3] then
        HakfooSkills(player, "yihushaliangyang", data)
      end
      if self.cost_data[4] then
        HakfooSkills(player, "longjuanfeng", data)
      end
      if self.cost_data[5] then
        HakfooSkills(player, "fulaminggewubu", data)
      end
      if self.cost_data[6] then
        player.room:notifySkillInvoked(player, "wuyazuofeiji", "big")
        local all_choices = {"heihutaoxin", "jufukandashu", "yihushaliangyang", "longjuanfeng", "fulaminggewubu"}
        local choice = player.room:askForChoice(player, all_choices, self.name)
        HakfooSkills(player, choice, data)
      end
    else
      local room = player.room
      if data.extra_data and data.extra_data.yihushaliangyang and data.extra_data.yihushaliangyang > 0 then
        local tos = player.room:askForChoosePlayers(player, U.getUseExtraTargets(room, data, false, true), 1, data.extra_data.yihushaliangyang, "#yihushaliangyang", self.name, true)
        data.extra_data.yihushaliangyang = 0
        if #tos > 0 then
          for _, pid in ipairs(tos) do
            AimGroup:addTargets(room, data, pid)
          end
        end
      end
      if data.extra_data and data.extra_data.longjuanfeng and data.extra_data.longjuanfeng > 0 then
        local to = room:getPlayerById(data.to)
        if #to:getCardIds("ej") > 0 then
          local cards = room:askForCardsChosen(player, to, 1, data.extra_data.longjuanfeng, "ej", self.name)
          room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true)
        end
      end
    end
  end,

  refresh_events = {fk.AfterCardUseDeclared},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@$lzsj__zoubian")
    local record = table.simpleClone(mark)
    if #mark > 1 then
      table.remove(mark, 1)
    end
    table.insert(mark, data.card:getEffectiveId())
    player.room:setPlayerMark(player, "@$lzsj__zoubian", mark)
    U.clearHandMark(player, "@@heihutaoxin-inhand")
    U.clearHandMark(player, "@@jufukandashu-inhand")
    U.clearHandMark(player, "@@yihushaliangyang-inhand")
    U.clearHandMark(player, "@@longjuanfeng-inhand")
    U.clearHandMark(player, "@@fulaminggewubu-inhand")
    U.clearHandMark(player, "@@wuyazuofeiji-inhand")
    local cardnames = {"savage_assault", "archery_attack", "god_salvation", "iron_chain", "lure_tiger", "await_exhausted", "collaborate"}
    for _, id in ipairs(player:getCardIds("h")) do
      local cd = Fk:getCardById(id)
      if cd.trueName == "slash" and data.card.suit == Card.Spade then
        room:setCardMark(cd, "@@heihutaoxin-inhand", 1)
      end
      if cd.trueName == "slash" and data.card.type == Card.TypeEquip then
        room:setCardMark(cd, "@@jufukandashu-inhand", 1)
      end
      if (cd.type == Card.TypeBasic or cd:isCommonTrick()) and not table.contains(cardnames, cd.trueName) and data.card.color == Card.Red then
        room:setCardMark(cd, "@@yihushaliangyang-inhand", 1)
      end
      if (cd.type == Card.TypeBasic or cd:isCommonTrick()) and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) then
        room:setCardMark(cd, "@@longjuanfeng-inhand", 1)
      end
      if #mark == 2 and cd.trueName == "jink" and data.card.color ~= Fk:getCardById(mark[1]).color then
        room:setCardMark(cd, "@@fulaminggewubu-inhand", 1)
      end 
      if #mark == 2 and Fk:getCardById(mark[1]).type ~= Fk:getCardById(mark[2]).type and
      table.every(mark, function (cid) return Fk:getCardById(cid).type ~= cd.type end) then
        room:setCardMark(cd, "@@wuyazuofeiji-inhand", 1)
      end
    end
    data.extra_data = data.extra_data or {}
    if #record == 2 and data.card.trueName == "jink" and Fk:getCardById(record[1]).color ~= Fk:getCardById(record[2]).color then
      data.extra_data.fulaminggewubu = true
    end 
    if #record == 2 and Fk:getCardById(record[1]).type ~= Fk:getCardById(record[2]).type and
    table.every(record, function (cid) return Fk:getCardById(cid).type ~= data.card.type end) then
      data.extra_data.wuyazuofeiji = true
    end
  end,
}

hakfoo:addSkill(lzsj__zoubian)

Fk:loadTranslationTable{
  ["lzsj__hakfoo"] = "阿福",
  ["designer:lzsj__hakfoo"] = "懂太师",
  ["lzsj__zoubian"] = "揍扁",
  [":lzsj__zoubian"] = "你使用牌可同时触发多个本回合未触发过的连招，且触发连招的牌不计入次数限制:"..
  "<br/><font color=#E49B0F><b>黑虎掏心</b></font>(黑桃牌+【杀】)∶获得对方一张手牌；"..
  "<br/><font color=#E49B0F><b>巨斧砍大树</b></font>(装备牌+【杀】)∶额外结算一次；"..
  "<br/><font color=#E49B0F><b>一虎杀两羊</b></font>(红色牌+单体即时牌):可额外选择一个目标；"..
  "<br/><font color=#E49B0F><b>龙卷风摧毁停车场</b></font>(即时牌+即时牌)∶弃置所有目标角色场上一张牌；"..
  "<br/><font color=#E49B0F><b>弗拉明戈舞步</b></font>(两种颜色的牌+【闪】)∶移动当前回合角色场上一张牌；"..
  "<br/><font color=#E49B0F><b>乌鸦坐飞机</b></font>(三种类别的牌)∶执行上述一个连招效果（无本回合未触发过的限制）。",
  ["@$lzsj__zoubian"] = "连招记录",
  ["@@heihutaoxin-inhand"] = '<font color="#B2BEB5"><b>黑虎</b></font>',
  ["@@jufukandashu-inhand"] = '<font color="#50C878">巨斧</b></font>',
  ["@@yihushaliangyang-inhand"] = '<font color="#E5AA70">两羊</b></font>',
  ["@@longjuanfeng-inhand"] = '<font color="#7393B3">龙卷</b></font>',
  ["@@fulaminggewubu-inhand"] = '<font color="#F2D2BD">弗拉</b></font>',
  ["@@wuyazuofeiji-inhand"] = '<font color="#CCCCFF">乌鸦</b></font>',

  ["heihutaoxin"] = "黑虎掏心",
  ["jufukandashu"] = "巨斧砍大树",
  ["yihushaliangyang"] = "一虎杀两羊",
  ["longjuanfeng"] = "龙卷风摧毁停车场",
  ["fulaminggewubu"] = "弗拉明戈舞步",
  ["wuyazuofeiji"] = "乌鸦坐飞机",

  ["#fulaminggewubu-ask"] = "弗拉明戈舞步：移动%src场上一张牌",
  ["#yihushaliangyang"] = "一虎杀两羊：请选择额外选择的目标",
}

local dongxuelian = General(extension, "lzsj__dongxuelian", "ev", 3)

local huahaichitu = fk.CreateTriggerSkill{
  name = "huahaichitu",
  anim_type = "support",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.card.color == Card.Red and player:usedSkillTimes(self.name) == 0
    and (data.card.is_damage_card or table.contains({"peach", "pear", "analeptic", "god_salvation"}, data.card.trueName))
    and player:getMark("@huahaichitu") < 4
  end,
  on_use = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@huahaichitu")
    if data.card.is_damage_card then
      data.additionalDamage = (data.additionalDamage or 0) + 1
    elseif table.contains({"peach", "pear", "god_salvation"}, data.card.trueName)
    or (data.card.trueName == "analeptic" and data.extra_data and data.extra_data.analepticRecover) then
      data.additionalRecover = (data.additionalRecover or 0) + 1
    end
  end,

  refresh_events = {fk.TurnStart},
	can_refresh = function(self, event, target, player, data)
		return player:hasSkill(self) and target == player and player:getMark("@huahaichitu") > 0
	end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local phases = {Player.Judge, Player.Draw, Player.Play, Player.Discard}
    for i = 1, 4 do
      if player:getMark("@huahaichitu") == 0 then break end
      room:removePlayerMark(player, "@huahaichitu")
      player:skip(phases[i])
    end
  end, 
}

local oujiezhugu = fk.CreateTriggerSkill{
  name = "oujiezhugu",
  events = {fk.EnterDying, fk.AfterSkillEffect},
  frequency = Skill.Limited,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player and player:isWounded() and (player.room:getBanner("oujiezhugu") == nil
    or not table.contains(player.room:getBanner("oujiezhugu"), player.id)) then
      if event == fk.AfterSkillEffect then
        return target == player and data == huahaichitu and player.room.current and player.room.current == player
      elseif event == fk.EnterDying then
        return true
      end
    end
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    --player._splayer:setDied(false)
    room:setPlayerRest(player, player:getLostHp())
    room:killPlayer({ who = player.id })
    if not player.room:getBanner("oujiezhugu") then
      room:setBanner("oujiezhugu", {player.id})
    else
      local banner = player.room:getBanner("oujiezhugu")
      table.insert(banner, player.id)
      room:setBanner("oujiezhugu", banner)
    end
  end,
}

Fk:addPoxiMethod{
  name = "lszj__shelie",
  card_filter = function(to_select, selected, data)
    if table.contains(data[2], to_select) then return true end
    local suit = Fk:getCardById(to_select).suit
    return table.every(data[2], function (id)
      return Fk:getCardById(id).suit ~= suit
    end)
  end,
  feasible = function(selected)
    return true
  end,
}

local bixuedanxin = fk.CreateTriggerSkill{
  name = "bixuedanxin",
  events = {fk.RoundEnd, fk.AfterPlayerRevived},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self, true, true) then
      if event == fk.RoundEnd then
        return player:getMark("phase_skipped-round") > 0 or player.rest > 0
      elseif event == fk.AfterPlayerRevived then
        return target == player
      end
    end
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundEnd then
      local cards = room:getNCards(5)
      room:moveCards({
        ids = cards,
        toArea = Card.Processing,
        moveReason = fk.ReasonPut,
        skillName = self.name,
        proposer = player.id
      })
      local get = {}
      for _, id in ipairs(cards) do
        local suit = Fk:getCardById(id).suit
        if table.every(get, function (id2)
          return Fk:getCardById(id2).suit ~= suit
        end) then
          table.insert(get, id)
        end
      end
      get = room:askForArrangeCards(player, self.name, cards, "#shelie-choose", false, 0, {5, 4}, {0, #get}, ".", "lszj__shelie", {{}, get})[2]
      if #get > 0 then
        room:obtainCard(player, get, true, fk.ReasonPrey)
      end
      cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
      if #cards > 0 then
        room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonJustMove, self.name)
      end
    elseif event == fk.AfterPlayerRevived then
      room:addPlayerMark(player, "bixuedanxin_targetmod")
      player:gainAnExtraTurn(true)
    end
  end,

  refresh_events = {fk.EventPhaseSkipped},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "phase_skipped-round")
  end,
}

local bixuedanxin_tar = fk.CreateTargetModSkill{
  name = "#bixuedanxin_targetmod",
  residue_func = function(self, player, skill, scope, card)
    if player:getMark("bixuedanxin_targetmod") > 0 and card and card.color == Card.Red then
      return 999
    end
  end,
  bypass_distances = function(self, player, skill, card, to)
    return player:getMark("bixuedanxin_targetmod") > 0 and card and card.color == Card.Red
  end,
}

bixuedanxin:addRelatedSkill(bixuedanxin_tar)
dongxuelian:addSkill(huahaichitu)
dongxuelian:addSkill(oujiezhugu)
dongxuelian:addSkill(bixuedanxin)

Fk:loadTranslationTable{
  ["lzsj__dongxuelian"] = "东雪莲",
  ["designer:lzsj__dongxuelian"] = "秦心",
  ["cv:lzsj__dongxuelian"] = "AI东雪莲",
  ["huahaichitu"] = "花海赤途",
  [":huahaichitu"] = "回合技，当一名角色使用红色牌时，你可按顺序跳过下回合的一个主要阶段，令之伤害或回复值+1。",
  ["@huahaichitu"] = "<font color='#C41E3A'><b>花海赤途</b></font>",
  ["oujiezhugu"] = "藕节铸骨",
  [":oujiezhugu"] = "限定技，你进入濒死状态后或于回合内发动“花海赤途”后，若你已受伤，可进入重整状态X轮（X为你已损失的体力值）。",
  ["bixuedanxin"] = "碧血丹心",
  [":bixuedanxin"] = "锁定技，轮次结束时，若你此轮跳过阶段或重整，你可发动一次“涉猎”，你结束重整后进行一个额外的回合，且你本局游戏使用红色牌无距离和次数限制。",
  ["shelie"] = "涉猎",
  ["#shelie-choose"] = "涉猎：获得不同花色的牌各一张",
  ["$huahaichitu1"] = "家人们！这波打赏再不到300棺…我就真成罕见了啊！",
  ["$huahaichitu2"] = "感谢老铁的火箭！棺材板今日买一送三！",
  ["$oujiezhugu1"] = "警告！藕饼CP连接失败…正在启动备用模块…",
  ["$oujiezhugu2"] = "感谢塔菲酱の棺材铺订单！本莲の数据…永不404！",
  ["$bixuedanxin1"] = "这波自爆卡车…值了！",
  ["$bixuedanxin2"] = "棺人痴の血条…由我来守护！",
}


return extension